package com.songcha.module_bookdetail.ui.dialog

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import com.alibaba.android.arouter.launcher.ARouter
import com.songcha.library_base.handler.ApiObservableHandler
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_business.provider.IDailyTaskProvider
import com.songcha.library_business.proxy.database.BookTodayCommentWriteCountDaoManager
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.ui.view.CustomButton
import com.songcha.library_common.ui.view.CustomEditText
import com.songcha.library_common.ui.view.RatingView
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_network.bean.BaseBean
import com.songcha.module_bookdetail.R
import com.songcha.module_bookdetail.event.CommentReportSuccessEvent
import com.songcha.module_task.api.BookDetailApiManager
import okhttp3.RequestBody.Companion.toRequestBody
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

class CommentWriteDialog(context: Context,val mBookId:String):CustomDialog(context) {

    private lateinit var mView:View

    init {
        setWidth(ScreenUtil.getScreenWidth())
        setDialogTitle("")
        setDialogMsg("")
        setGravity(Gravity.BOTTOM)
        setAnim(com.songcha.library_common.R.style.dialogBottomSlideWindowAnim)
        setShowBtn(false)
        setRadius(floatArrayOf(5f.dp2px(),5f.dp2px(),0f,0f))
    }

    override fun hasView(): Boolean {
        return true
    }

    override fun getView(root: ViewGroup): View? {
        mView= LayoutInflater.from(context).inflate(R.layout.bookdetail_dialog_comment_write,null)
        val lp= FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT,430.dp2px())
        /*lp.leftToLeft= ConstraintLayout.LayoutParams.PARENT_ID
        lp.rightToRight= ConstraintLayout.LayoutParams.PARENT_ID
        lp.bottomToBottom= ConstraintLayout.LayoutParams.PARENT_ID*/
        mView.layoutParams=lp

        return mView

    }



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mView.findViewById<ImageView>(R.id.bookdetail_iv_close).setOnClickListener {
            dismiss()
        }
        mView.findViewById<CustomButton>(R.id.bookdetail_btn_write_comment).setOnClickListener {
            writeComment()
        }
    }

    private fun writeComment(){
        val score=mView.findViewById<RatingView>(R.id.bookdetail_rating).getScore()
        val content=mView.findViewById<CustomEditText>(R.id.bookdetail_edt_write_comment).text.toString().trim()
        if(content.isBlank()){
            ToastUtil.show("请输入评论内容")
            return
        }
        if(content.length>200){
            ToastUtil.show("评论内容不能超过200个字符")
            return
        }
        if(!UserInfoManager.checkJumpLogin()) return
        apiWriteComment(score,content)
    }

    private fun apiWriteComment(score:Int,content:String){
        val json=JSONObject().apply {
            put("bookId",mBookId)
            put("starNumber",score)
            put("content",content)
        }
        ApiObservableHandler.handle(BookDetailApiManager.api.addBookComment(json.toString().toRequestBody()),object :IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {
                ToastUtil.show("感谢您的评论")
                dismiss()

                EventBus.getDefault().post(CommentReportSuccessEvent())

                val count=BookTodayCommentWriteCountDaoManager.addTodayCommentWriteCount()
                if(count shr 31==-1)
                    ARouter.getInstance().build("/task/DailyTaskController").navigation()?.let {
                        it as IDailyTaskProvider
                        it.handleDailyTask(IDailyTaskProvider.DailyType.WRITE_COMMENT,count)
                    }
            }

        })

        /*BookDetailApiManager.api.addBookComment(json.toString().toRequestBody())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseBean>{
                override fun onSubscribe(d: Disposable) {

                }

                override fun onError(e: Throwable) {
                }

                override fun onComplete() {

                }

                override fun onNext(t: BaseBean) {
                    if(t.code==ApiCode.API_SUCCESS_CODE){
                        ToastUtil.show("感谢您的评论")
                        dismiss()
                    }else{
                        ToastUtil.show(t.msg)
                    }
                }

            })*/


    }



}